/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MyAccount.java
 *
 * 
 */
package UI;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import Model.User;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
/**
 *
 * @author Administrator
 */
public class MyAccount extends javax.swing.JDialog {
    
    ObjectOutputStream out;
    ObjectInputStream in;  
     DefaultTableModel model;
     private User myAccount;
       Socket cs;
    /** Creates new form MyAccount2 */
    public MyAccount(java.awt.Frame parent, boolean modal,ObjectOutputStream out,ObjectInputStream in) {
        super(parent, modal);
   

            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

            } catch (ClassNotFoundException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            initComponents();
           
                //mo luong out,in de goi nhan du lieu voi server
              
            this.out = out; this.in = in;
             model = (DefaultTableModel) tblAccount.getModel();
             myAccount = new User();
            loadData();
           
        
    }

//    *************load data on table Account*************
    public void loadData() {
        try {
 
            String username    = MainFrame.Username;
            
                 
            out.writeObject("showMyAccount");
            out.writeObject(username);
         
              myAccount = (User) in.readObject();
            while(tblAccount.getRowCount()>1)
               model.removeRow(tblAccount.getRowCount() - 1);
             model.insertRow(0, myAccount.toVector());
              txtUsername.setText(myAccount.getUsername());
               
            
        
            
        } catch (ClassNotFoundException | IOException ex) {
            System.out.println(ex);
        }
    }

  
    //************ method exit form**************
    public void cancel() {
        int option = JOptionPane.showConfirmDialog(this, "Are you sure want to exit ?","Exit",JOptionPane.OK_CANCEL_OPTION);     
        if (option == JOptionPane.OK_OPTION) {
            this.setVisible(false);
            this.dispose();
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnMain = new javax.swing.JPanel();
        lblPicture = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblAccount = new javax.swing.JTable();
        jLabel10 = new javax.swing.JLabel();
        pnChangeAccount = new javax.swing.JPanel();
        lblUsername = new javax.swing.JLabel();
        lblOldPassword = new javax.swing.JLabel();
        lblNewPassword = new javax.swing.JLabel();
        lblConfirmPassword = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        btnChange = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        pwfOldPassword = new javax.swing.JPasswordField();
        pwfNewPassword = new javax.swing.JPasswordField();
        pwfConfirmPass = new javax.swing.JPasswordField();
        jLabel11 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Account Management System");
        setResizable(false);

        pnMain.setBackground(new java.awt.Color(0, 215, 255));
        pnMain.setPreferredSize(new java.awt.Dimension(800, 600));

        lblPicture.setBackground(new java.awt.Color(255, 0, 0));
        lblPicture.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/MyAccount.png"))); // NOI18N

        jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.LEFT);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Information Account", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        tblAccount.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Username", "Password", "Role"
            }
        ));
        jScrollPane1.setViewportView(tblAccount);

        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/LogoDVT.gif"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 324, Short.MAX_VALUE)
                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jTabbedPane1.addTab("Information Account", new javax.swing.ImageIcon(getClass().getResource("/Images/myaccounticon.png")), jPanel1); // NOI18N

        pnChangeAccount.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Change Account", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        lblUsername.setText("Username:");

        lblOldPassword.setText("Old Password");

        lblNewPassword.setText("New Password:");

        lblConfirmPassword.setText("Confirm Password");

        txtUsername.setEditable(false);

        btnChange.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Edit.png"))); // NOI18N
        btnChange.setText("Change");
        btnChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeActionPerformed(evt);
            }
        });

        btnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/reset.png"))); // NOI18N
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Exit.png"))); // NOI18N
        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/LogoDVT.gif"))); // NOI18N

        javax.swing.GroupLayout pnChangeAccountLayout = new javax.swing.GroupLayout(pnChangeAccount);
        pnChangeAccount.setLayout(pnChangeAccountLayout);
        pnChangeAccountLayout.setHorizontalGroup(
            pnChangeAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnChangeAccountLayout.createSequentialGroup()
                .addGap(69, 69, 69)
                .addGroup(pnChangeAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblUsername)
                    .addComponent(lblOldPassword)
                    .addComponent(lblNewPassword)
                    .addComponent(lblConfirmPassword))
                .addGap(59, 59, 59)
                .addGroup(pnChangeAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pwfConfirmPass)
                    .addComponent(pwfNewPassword)
                    .addComponent(pwfOldPassword)
                    .addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
                .addComponent(jLabel11))
            .addGroup(pnChangeAccountLayout.createSequentialGroup()
                .addGap(106, 106, 106)
                .addComponent(btnChange)
                .addGap(31, 31, 31)
                .addComponent(btnReset)
                .addGap(40, 40, 40)
                .addComponent(btnExit))
        );
        pnChangeAccountLayout.setVerticalGroup(
            pnChangeAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnChangeAccountLayout.createSequentialGroup()
                .addGroup(pnChangeAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnChangeAccountLayout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(pnChangeAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblUsername))
                        .addGap(48, 48, 48)
                        .addGroup(pnChangeAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblOldPassword)
                            .addComponent(pwfOldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(31, 31, 31)
                        .addGroup(pnChangeAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblNewPassword)
                            .addComponent(pwfNewPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(30, 30, 30)
                        .addGroup(pnChangeAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblConfirmPassword)
                            .addComponent(pwfConfirmPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(50, 50, 50)
                        .addGroup(pnChangeAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnChange)
                            .addComponent(btnReset)
                            .addComponent(btnExit)))
                    .addGroup(pnChangeAccountLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(181, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Change Password    ", new javax.swing.ImageIcon(getClass().getResource("/Images/changeAccount.png")), pnChangeAccount); // NOI18N

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/BannerDVT.gif"))); // NOI18N

        javax.swing.GroupLayout pnMainLayout = new javax.swing.GroupLayout(pnMain);
        pnMain.setLayout(pnMainLayout);
        pnMainLayout.setHorizontalGroup(
            pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnMainLayout.createSequentialGroup()
                .addComponent(lblPicture)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 672, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 816, Short.MAX_VALUE)
        );
        pnMainLayout.setVerticalGroup(
            pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnMainLayout.createSequentialGroup()
                .addGroup(pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblPicture, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 544, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnMain, javax.swing.GroupLayout.DEFAULT_SIZE, 816, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnMain, javax.swing.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeActionPerformed
        // TODO add your handling code here:
        String newPassword = new String( pwfNewPassword.getPassword());
        String confirmPassword = new String (pwfConfirmPass.getPassword());
        String oldPasswod = new String (pwfOldPassword.getPassword());
        if(!newPassword.equals(confirmPassword))
        {
            JOptionPane.showMessageDialog(this, "New password and confirm password isn't same.Plase try again");
            return;
        }
        if(!myAccount.getPassword().equals(oldPasswod))
        {
            JOptionPane.showMessageDialog(this, "Old password is not correct .Plase try again");
            return;
        }
        myAccount.setPassword(newPassword);
        try {
            out.writeObject("changePassword");
            out.writeObject(myAccount);
            boolean alert = (Boolean)in.readObject();
           if(alert)
           {
                 JOptionPane.showMessageDialog(this, "Change Password successfully");
                 

                  myAccount.setPassword(newPassword);
                  model.setValueAt(newPassword, 0, 1);

           }
           else 
                JOptionPane.showMessageDialog(this, "Change Password failed");
        } catch (IOException|ClassNotFoundException ex) {
           System.out.println(ex);
        }
        
    }//GEN-LAST:event_btnChangeActionPerformed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        // TODO add your handling code here:
        cancel();        
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        // TODO add your handling code here:
        pwfConfirmPass.setText("");
        pwfNewPassword.setText("");
        pwfOldPassword.setText("");
    }//GEN-LAST:event_btnResetActionPerformed
//*

    /**
     * @param args the command line arguments
     */
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChange;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnReset;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lblConfirmPassword;
    private javax.swing.JLabel lblNewPassword;
    private javax.swing.JLabel lblOldPassword;
    private javax.swing.JLabel lblPicture;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JPanel pnChangeAccount;
    private javax.swing.JPanel pnMain;
    private javax.swing.JPasswordField pwfConfirmPass;
    private javax.swing.JPasswordField pwfNewPassword;
    private javax.swing.JPasswordField pwfOldPassword;
    private javax.swing.JTable tblAccount;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
